<?php
namespace Home\Model;
use Think\Model\ViewModel;

class  DpQuestionCategoryModel extends ViewModel {

    static private $_instance = null;
    private $allCategoryTree;
    private $allCategoryData;
    private $model;

    static public function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new DpQuestionCategoryModel();
            self::$_instance->allCategoryTree = self::$_instance->getCategoryTree();
        }
        return self::$_instance;
    }

    /**
     * @return mixed
     */
    public function getAllCategoryData()
    {
        return $this->allCategoryData;
    }

    /**
     * @return mixed
     */
    public function getAllCategoryTree()
    {
        return $this->allCategoryTree;
    }

    /**
     * 获取问题种类清单-树形结构
     */
   private function getCategoryTree(){

        $model = M('dpQuestionCategory');
        $this->model = $model;
       $result = $model->select();
        $this->allCategoryData = $result;

        $tree = array();
        foreach ($result as $key => $value)
        {
            // $key : 0, 1, 2, 3,
            // $value : array with key
            if ($value['parent_category_id'] != 0){
                $tree[] = array ('id'=>$value['id'],'text'=>$value['name'],'pid'=>$value['parent_category_id']);
            }else{
                $tree[] = array ('id'=>$value['id'],'text'=>$value['name'],'checked'=>'true');
            }
        }
        return $tree;
    }

    public function getParentCategoryByID($id)
    {
        $tmpList = $this->_getParentCategoryByID($id);
        $categoryList = substr($tmpList,0,strlen($tmpList)-1);
        if($categoryList) {
            $categoryList = $id . ',' . $categoryList;
        }else{
            $categoryList = $id;
        }
        return $categoryList;
    }

    private function _getParentCategoryByID($id)
    {
        $categoryList = "";
        foreach ($this->allCategoryTree as $key => $item)
        {
            if($item['id'] == $id)
            {
                // recursion
                if(!empty($item['pid'])) {
                    $categoryList .= $item['pid'] . ',';
                    $categoryList .= $this->_getParentCategoryByID($item['pid']);
                }
            }
        }
        return $categoryList;
    }

    public function getCNCategoryChainbyIDList($idList){
       //var_dump($idList);die();
        $idArray = explode(',',$idList);
        //var_dump($idArray);die();
        $cnChain = '';
        foreach ($this->allCategoryTree as $key => $item){
            //var_dump($item);
            foreach ($idArray as $inKey => $inItem)
            {
                if($item['id'] == $inItem)
                {
                    //die();
                    $cnChain .= $item['text'].'>';
                }

            }
       }
        return $cnChain;
    }

    public function updateQuestionCategory($data){

        foreach ($data as $key => $rowData){
            if ($rowData['_state'] == "removed"){
                $this->model->where("id = {$rowData['id']}")->delete();
            }
            if ($rowData['_state'] == "added"){
                $this->model->add($rowData);
            }
            if ($rowData['_state'] == "modified"){
                $this->model->save($rowData);
            }
        }

    }

}